package edu.pku.sei.modeler.neditor.part;

import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.gef.EditPolicy;

import edu.pku.notation.runtime.constance.ColorConstance;
import edu.pku.sei.gefcommon.common.resourceManager.ResourceGroupManager;
import edu.pku.sei.modeler.model.notation.shape.Shape;
import edu.pku.sei.modeler.neditor.part.policy.NodeContainerLayoutEditPolicy;
import edu.pku.sei.modeler.neditor.part.policy.NodeDeleteEditPolicy;

public class PolylineEditPart extends AbstractMetaElementPart {

	@Override
	protected IFigure createFigure() {
		IFigure figure = null;
		edu.pku.sei.modeler.model.notation.shape.Polyline model = (edu.pku.sei.modeler.model.notation.shape.Polyline)getModel();
		switch(model.getType()){
		case edu.pku.sei.modeler.model.notation.shape.Polyline.VERTICAL:
			figure = edu.pku.notation.runtime.draw2d.PolylineFigure.createVerticalLineFigure();
			break;
		case edu.pku.sei.modeler.model.notation.shape.Polyline.HORIZONTAL:
			figure = edu.pku.notation.runtime.draw2d.PolylineFigure.createHorizontalLineFigure();
			break;
		default:
			System.out.println("crash!!");
			figure = new org.eclipse.draw2d.RectangleFigure();
		}
		
		figure.setLayoutManager(new XYLayout());
		return figure;	}

	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.CONTAINER_ROLE,new NodeContainerLayoutEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE,new NodeDeleteEditPolicy());
	}
	@Override
	protected List getModelChildren() {
		return ((Shape)getModel()).getSlaves();
	}
	@Override
	protected void refreshVisuals() {
		edu.pku.notation.runtime.draw2d.PolylineFigure figure = (edu.pku.notation.runtime.draw2d.PolylineFigure)getFigure();
		edu.pku.sei.modeler.model.notation.shape.Polyline model = (edu.pku.sei.modeler.model.notation.shape.Polyline)getModel();
		figure.setBounds(model.getBound());	
		figure.setForegroundColor(ResourceGroupManager.getSWTColor(model.getLineColor()));
		figure.setBackgroundColor(ResourceGroupManager.getSWTColor(model.getFillColor()));
		
		figure.refresh();
		model.doLayout();
	}
}
